//
//  HMComposeView.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/28.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMComposeView.h"
#import "HMGlobalConst.h"
#import "UIImage+ImageEffects.h"
#import "HMComposeButton.h"
#import <POP.h>
#import "HMComposeMenu.h"
#import <YYModel.h>

#import "HMComposeViewController.h"
#import "HMNavigationViewController.h"

#import "HMWeiBoOC-Swift.h" 
@interface HMComposeView ()

@property (nonatomic,strong) UIImageView *sloganImageView;

@property (nonatomic,strong) HMComposeButton *composeBtn;

@property (nonatomic,strong) NSMutableArray *buttonArray;

@property (nonatomic,strong) UIViewController *targetVc;

@property (nonatomic,strong) NSArray<HMComposeMenu *> *composeMenuArray;

@end


@implementation HMComposeView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
//        CGSize size = self.frame.size;
//        size = CGSizeMake(ScreenWidth, ScreenHeight);
//        
//        self.frame.size = size;
        
        _buttonArray = [NSMutableArray array];
        self.composeMenuArray = [self loadComposePlistData];
        [self setupUI];
        
    }
    return self;
}

-(void)setupUI{
    self.backgroundColor = [self randomColor];
    
    UIImage *image = [UIImage screenShot].applyLightEffect;
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:image];

    [self addSubview:bgImageView];
    
    [self addSubview:self.sloganImageView];
    [_sloganImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(100);
        make.centerX.equalTo(self);
    }];
    
//    [_composeBtn ];
    [self addChildBtn];
    [self startAnimation:YES];
    
}

-(void)startAnimation:(BOOL)isUp{
//    if (isUp == NO) {
//        self.buttonArray = [self.buttonArray reverseObjectEnumerator];
//    }
    
    
//    if (isUp) {
//        
//        [self.buttonArray enumerateObjectsUsingBlock:^(HMComposeButton * obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
//            anim.toValue = [NSValue valueWithCGPoint:CGPointMake(obj.center.x, obj.center.y - 350)];
//            anim.beginTime = CACurrentMediaTime() + idx * 0.025;
//            anim.removedOnCompletion = NO;
//            // 向前填充-> 留在最终的位置!
//            anim.fillMode = kCAFillModeForwards;
//            [obj.layer addAnimation:anim forKey:nil];
//        }]; 
//    }
    
    if (isUp) {
        
        [self.buttonArray enumerateObjectsUsingBlock:^(HMComposeButton * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            POPSpringAnimation *popAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
            
            popAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(obj.center.x, obj.center.y - 350)];
            popAnimation.springSpeed = 10;
            popAnimation.springBounciness = 10;
            popAnimation.beginTime = CACurrentMediaTime() + idx * 0.025;
            
            [obj pop_addAnimation:popAnimation forKey:nil];
        }];
    }
//
//        for (int idx = 0; idx < self.buttonArray.count; idx++) {
//            
//            HMComposeButton *obj = self.buttonArray[idx];
//            POPSpringAnimation *popAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
//            
//            popAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(obj.center.x, obj.center.y - 350)];
//            popAnimation.springSpeed = 10;
//            popAnimation.springBounciness = 10;
//            popAnimation.beginTime = CACurrentMediaTime() + idx * 0.025;
//            
//            [obj pop_addAnimation:popAnimation forKey:nil];
//        }
        
//    }
}

-(void)addChildBtn{
    CGFloat btnW = 80;
    CGFloat btnH = 110;
    CGFloat btnMargin = (self.frame.size.width - 3 * btnW)/4;
    
    for (int i =0; i < self.composeMenuArray.count; i ++) {//循环创建按钮,不能用懒加载
        HMComposeButton *composeBtn = [[HMComposeButton alloc] init];
        [composeBtn addTarget:self action:@selector(composeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [composeBtn setImage:[UIImage imageNamed:self.composeMenuArray[i].icon] forState:UIControlStateNormal];
        [composeBtn setTitle:self.composeMenuArray[i].title forState:UIControlStateNormal];
        int colIndex = i % 3;
        int rowIndex = i /3;
        
        CGFloat btnX = colIndex * btnW + (colIndex + 1) * btnMargin;
        CGFloat btnY = rowIndex *btnH + (rowIndex + 1 ) * btnMargin + ScreenHeight;
        
        composeBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        
        [self addSubview:composeBtn];
        [self.buttonArray addObject:composeBtn];
    }
}

-(void)composeBtnClick:(HMComposeButton *)btn{
    [UIView animateWithDuration:0.25 animations:^{
        for (HMComposeButton *btn in self.buttonArray) {
            btn.alpha = 0.2;
            if ([btn isEqual: btn]) {
                btn.transform = CGAffineTransformScale(btn.transform, 2, 2);
            }else{
                btn.transform = CGAffineTransformScale(btn.transform, 0.2, 0.2);
            }
        }
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.25 animations:^{
            for (HMComposeButton *btn in self.buttonArray) {
                btn.transform = CGAffineTransformIdentity;
                btn.alpha = 1;
            }

        } completion:^(BOOL finished) {
            NSLog(@"test");
            HMComposeViewController *composeVc = [[HMComposeViewController alloc] init];
            HMNavigationViewController *nav = [[HMNavigationViewController alloc] initWithRootViewController:composeVc];
            [self.targetVc presentViewController:nav animated:YES completion:^{
                [self removeFromSuperview];
            }];
        }];
        
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self removeFromSuperview];
//    [self startAnimation:NO];
}

-(void)showComposeView:(UIViewController *)target{
    self.targetVc = target;
    
    [target.view addSubview:self];
}


#pragma mark - getter

-(UIImageView *)sloganImageView{
    if (!_sloganImageView) {
        _sloganImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"compose_slogan"]];
    }
    return _sloganImageView;
}

//-(HMComposeButton *)composeBtn{
//    if (_composeBtn == nil) {
//        _composeBtn = [[HMComposeButton alloc] init];
//        _composeBtn.frame = CGRectMake(0, 0, 80, 110);
//        [_composeBtn setImage:[UIImage imageNamed:@"tabbar_compose_idea"] forState:UIControlStateNormal];
//        [_composeBtn setTitle:@"文字" forState:UIControlStateNormal];
//    }
//    return _composeBtn;
//}


-(NSArray<HMComposeMenu *> *)loadComposePlistData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"compose.plist" ofType:nil];
    
    NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
    
    
    NSArray *modelArray = [NSArray yy_modelArrayWithClass:[HMComposeMenu class] json:dictArray];
    return modelArray;
}


- (UIColor *)randomColor {
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}


@end
